package cn.hftoday.org;

import cn.hftoday.core.ApplicationConfig;
import cn.hftoday.db.ConnectionPool;
import junit.framework.TestCase;
import cn.hftoday.util.*;
import cn.hftoday.core.*;

public class AccountTest extends TestCase {

	protected void setUp() throws Exception {
		ApplicationConfig.getInstance().load();
		ConnectionPool.getInstance().open();
		ConnectionPool.getInstance().beginTransaction();
	}

	protected void tearDown() throws Exception {
		ConnectionPool.getInstance().rollback();
		ConnectionPool.getInstance().close();
	}

	public void testGetId() {
		Account a = new Account();
		a.setId("abcefg");
		assertEquals(a.getId(), "abcefg");
	}

	public void testGetName() {
		Account a = new Account();
		a.setName("abcefg");
		assertEquals(a.getName(), "abcefg");
	}

	public void testGetEmail() {
		Account a = new Account();
		a.setEmail("abcefg");
		assertEquals(a.getEmail(), "abcefg");
	}

	public void testIsValid() {
		Account a = new Account();
		a.setValid(true);
		assertEquals(a.isValid(), true);
	}
	
	public void testGetPassword() {
		Account a = new Account();
		a.setPassword("abcefg");
		assertEquals(a.getPassword(), "abcefg");
	}

	public void testGetCreateTime() {
		Account a = new Account();
		a.setCreateTime(CalendarParser.toCalendar("2008-01-11 15:45:00"));
		assertEquals(a.getCreateTime(), CalendarParser.toCalendar("2008-01-11 15:45:00"));
	}

	public void testGetPerson() {
		//insert a person instance for test
		Person p = new Person();
		p.setId("22cc14f6-d0fc-4526-8966-99b37ebcd63a");
		p.setName("wenjiabao");
		Mapper.getInstance().insert("InsertPerson", p);
		
		//test account
		Account a = new Account();
		a.setPerson(p);
		
		Person p2 = a.getPerson();
		assertEquals(p2.getId(), "22cc14f6-d0fc-4526-8966-99b37ebcd63a"); 
		assertEquals(p2.getName(), "wenjiabao"); 
	}

	public void testGetCompany() {
		//insert a company instance for test
		Company c = new Company();
		c.setId("c764faf7-8270-46a2-b78c-62959ee48d95");
		c.setName("abcde");
		Mapper.getInstance().insert("InsertCompany", c);
		
		//test account
		Account a = new Account();
		a.setCompany(c);
		
		Company c2 = a.getCompany();
		assertEquals(c2.getId(), "c764faf7-8270-46a2-b78c-62959ee48d95"); 
		assertEquals(c2.getName(), "abcde"); 
	}
	
	public void testSelect() {
		testInsert();
		
		Account a = (Account)Mapper.getInstance().queryForObject("SelectAccountById", "23345678-1234-5678-12345678-12345678");
		assertEquals(a.getId(), "23345678-1234-5678-12345678-12345678");
		assertEquals(a.getName(), "luht22");
		assertEquals(a.getEmail(), "luht22@hftoday.cn");
		assertEquals(a.getPassword(), "123456");
		assertEquals(a.getCreateTime(), CalendarParser.toCalendar("2009-01-12 21:34:50"));
		assertEquals(a.isValid(), true);
	}

	public void testInsert() {
		Account acct = new Account();
		acct.setId("23345678-1234-5678-12345678-12345678");
		acct.setName("luht22");
		acct.setEmail("luht22@hftoday.cn");
		acct.setPassword("123456");
		acct.setCreateTime(CalendarParser.toCalendar("2009-01-12 21:34:50"));
		acct.setPerson(null);
		acct.setCompany(null);
		acct.setValid(true);
		Object o = Mapper.getInstance().insert("InsertAccount", acct);
		assertSame(acct, o);
	}
	
	public void testUpdate() {
		testInsert();
		Account a = (Account)Mapper.getInstance().queryForObject("SelectAccountById", "23345678-1234-5678-12345678-12345678");
		
		a.setPassword("654321");
		a.setValid(false);
		Mapper.getInstance().update("UpdateAccount", a);
		a = (Account)Mapper.getInstance().queryForObject("SelectAccountById", "23345678-1234-5678-12345678-12345678");
		assertEquals(a.getPassword(), "654321");
		assertEquals(a.isValid(), false);
	}
	
	public void testDelete() {
		testInsert();
		Account a = (Account)Mapper.getInstance().queryForObject("SelectAccountById", "23345678-1234-5678-12345678-12345678");
		
		Mapper.getInstance().delete("DeleteAccount", a);
		a = (Account)Mapper.getInstance().queryForObject("SelectAccountById", "23345678-1234-5678-12345678-12345678");
		assertEquals(a, null);
	}
}
